package ua.org.plast.mobileApp.navigation;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import ua.org.plast.mobileApp.R;
import ua.org.plast.mobileApp.fragments.BaseFragment;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Virus1908
 * Date: 30.09.13
 * Time: 21:00
 */
public class NavigationAdapter extends ArrayAdapter<NavigationItem> {
    private static final int LAYOUT = R.layout.navigation_item;

    public interface Switcher{
        void switchTo(Class<? extends BaseFragment> fragment);
    }

    private Switcher mSwitcher;
    LayoutInflater mInflater;

    public NavigationAdapter(Context context, List<NavigationItem> objects, Switcher switcher) {
        super(context, LAYOUT, objects);
        mSwitcher = switcher;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public NavigationAdapter(Context context, NavigationItem[] objects, Switcher switcher) {
        super(context, LAYOUT, objects);
        mSwitcher = switcher;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        ViewHolder holder;
        if (itemView == null) {
            itemView = mInflater.inflate(LAYOUT, null);
            holder = new ViewHolder();
            holder.icon = (ImageView) itemView.findViewById(R.id.icon);
            holder.title = (TextView) itemView.findViewById(R.id.title);
            holder.rightIcon = (ImageView) itemView.findViewById(R.id.right_icon);
            holder.subViews = (LinearLayout) itemView.findViewById(R.id.container);
            itemView.setTag(holder);
        } else {
            holder = (ViewHolder) itemView.getTag();
        }
        final NavigationItem item = getItem(position);
        holder.icon.setImageResource(item.icon);
        holder.title.setText(item.title);
        if (item.hasRightIcon()) {
            holder.rightIcon.setVisibility(View.VISIBLE);
            holder.rightIcon.setImageResource(item.rightIcon);
        } else {
            holder.rightIcon.setVisibility(View.GONE);
        }
        if (item.hasSubItems()) {
            holder.subViews.removeAllViews();
            for (int i = 0; i < item.subItems.length; i++) {
                final NavigationItem.SubItem subItem = item.subItems[i];
                View sub = mInflater.inflate(R.layout.navigation_sub_veiw, null);
                ((TextView) sub.findViewById(R.id.title)).setText(subItem.title);
                sub.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(mSwitcher != null){
                            mSwitcher.switchTo(subItem.launch);
                        }
                    }
                });
                holder.subViews.addView(sub);
            }
            if (item.isOpen) {
                holder.subViews.setVisibility(View.VISIBLE);
            } else {
                holder.subViews.setVisibility(View.GONE);
            }
        } else {
            holder.subViews.setVisibility(View.GONE);
        }
        return itemView;
    }

    public static class ViewHolder {
        public ImageView icon;
        public TextView title;
        public ImageView rightIcon;
        public LinearLayout subViews;
    }
}
